use std::io;

// fn greeting(name: String) {
//     println!("hello,{}", name)
// }
fn test_basic_str() {
    // let s = String::from("ss"); // 字符串类型, 可变
    // let ss = "ss"; //字符串切片类型，不可变

    let mut s1 = String::from("hi,rust!");
    s1.insert_str(8, "I like it!");

    println!("mut s1: {s1}")

}
fn test_slice() {
    let s = "hello,hyw";
    let length = s.len();
    // let hello = &s[0..5];
    let hello = &s[..5];
    let name = &s[6..9];
    let whole = &s[0..length];
    println!("first slice : {hello} second slice: {name}, the whole string is : {whole}");
}
fn test_tuple () {
    let tup: (i32,f64, &str) = (20, 33.2, "dcfcs");
    let (a, _b, _c ) = tup;
    println!("first tuple is : {}, equal to a: {a}", tup.0);

}
#[derive(Debug)]
struct User {
    is_actived: bool,
    username: String,
    email: String,
    token: u128
}
fn build_instance() -> User {
    User {
        is_actived: false,
        username: String::from("hww"),
        email: String::from("houyw@163.com"),
        token: 22
    }
}
fn test_struct() {
    let user = build_instance();
    println!("build user is: {:?}", user)
}
#[derive(Debug)]
enum Message {
    Quite,
    Move {
        x: i32,
        y: i32
    },
    Write(String),
    ChangeColor(i32, i32, i32)
}
fn print_message(s: Message) {
    println!("fn print_message: {:?}", s)
}
fn test_num () {
    let quite = Message::Quite;
    print_message(quite)
}
fn test_array() {
    let a = [1, 2, 3, 4, 5];
    // let a: [i32; 5] = [1, 2, 3, 4, 5]; 带类型声明
    println!("Please enter an array index.");

    let mut index = String::new();
    // 读取控制台的输出
    io::stdin()
        .read_line(&mut index)
        .expect("Failed to read line");

    let index: usize = index
        .trim()
        .parse()
        .expect("Index entered was not a number");

    let element = a[index];

    println!(
        "The value of the element at index {} is: {}",
        index, element
    );
}
fn test_ref_remove() {
    #[derive(Debug)]
    struct Person {
        name: String,
        age: u8,
    }

    let person = Person {
        name: String::from("Alice"),
        age: 20,
    };

    // `name` 从 person 中移走，但 `age` 只是引用
    let Person { name, ref age } = person;

    println!("The person's age is {}", age);

    println!("The person's name is {}", name);

    // 报错！部分移动值的借用：`person` 部分借用产生
    // println!("The person struct is {:?}", person);

    // `person` 不能使用，但 `person.age` 因为没有被移动而可以继续使用
    println!("The person's age from person struct is {}", person.age);
}

fn main() {
    // let name = "hyw";
    test_slice();
    test_basic_str();
    test_tuple();
    test_struct();
    test_num();
    test_array();
    test_ref_remove();
}
